package com.jianbing.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * Created by hantixiang on 2016/12/23.
 */
public class OperatorEngine {
    private static Log logger = LogFactory.getLog(OperatorEngine.class);
    public static ScriptEngine engine = null;

    public static Object eval(String expression) {
        try {
            if (engine == null) {
                synchronized (OperatorEngine.class) {
                    if (engine == null) {
                        ScriptEngineManager manager = new ScriptEngineManager();
                        engine = manager.getEngineByName("js");
                    }
                }
            }
            return engine.eval(expression);
        } catch (ScriptException e1) {
            logger.error(e1);
            return null;
        }
    }

}
